package com.yylending.las.common.utils;

import android.content.Context;
import android.telephony.TelephonyManager;
import com.yyfax.common.CLog;

import java.io.*;

/**
 * Created by laixj on 2017/7/26.
 * Copyright ©2017 www.yylending.com. All Rights Reserved.
 */
public class EmulatorChecker {
	static String TAG = "EmulatorChecker";

	private static String[] known_qemu_drivers = {"goldfish"};

	public static boolean checkQEmuDriverFile() {
		File driver_file = new File("/proc/tty/drivers");
		if (driver_file.exists() && driver_file.canRead()) {
			byte[] data = new byte[(int) driver_file.length()];
			try {
				InputStream inStream = new FileInputStream(driver_file);
				inStream.read(data);
				inStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			String driver_data = new String(data);
			for (String known_qemu_driver : known_qemu_drivers) {
				if (driver_data.indexOf(known_qemu_driver) != -1) {
					CLog.d(TAG, "Find known_qemu_drivers!");
					return true;
				}
			}
		}
		CLog.d(TAG, "Not Find known_qemu_drivers!");
		return false;
	}

	private static String[] known_numbers = {"15555215554", "15555215556",
			"15555215558", "15555215560", "15555215562", "15555215564",
			"15555215566", "15555215568", "15555215570", "15555215572",
			"15555215574", "15555215576", "15555215578", "15555215580",
			"15555215582", "15555215584",};

	public static boolean checkPhoneNumber(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

		String phoneNumber = telephonyManager.getLine1Number();
		CLog.d(TAG, "phonenumber="+phoneNumber);
		for (String number : known_numbers) {
			if (number.equalsIgnoreCase(phoneNumber)) {
				CLog.d(TAG, "Find PhoneNumber number="+number);
				return true;
			}
		}
		CLog.d(TAG, "Not Find PhoneNumber!");
		return false;
	}

	private static String[] known_device_ids = {"000000000000000"}; // 默认ID

	public static boolean checkDeviceIDS(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String device_ids = telephonyManager.getDeviceId();

		for (String know_deviceid : known_device_ids) {
			if (know_deviceid.equalsIgnoreCase(device_ids)) {
				CLog.d(TAG, "Find ids: 000000000000000!");
				return true;
			}
		}
		CLog.d(TAG, "Not Find ids: 000000000000000!");
		return false;
	}

	private static String[] known_imsi_ids = {"310260000000000"};// 默认的 imsi id

	public static boolean checkImsiIDS(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi_ids = telephonyManager.getSubscriberId();
		for (String know_imsi : known_imsi_ids) {
			if (know_imsi.equalsIgnoreCase(imsi_ids)) {
				CLog.d(TAG, "Find imsi ids: 310260000000000!");
				return true;
			}
		}
		CLog.d(TAG, "Not Find imsi ids: 310260000000000!");
		return false;
	}

	public static boolean checkEmulatorBuild(Context context) {
		String board = android.os.Build.BOARD;
		String bootloader = android.os.Build.BOOTLOADER;
		String brand = android.os.Build.BRAND;
		String device = android.os.Build.DEVICE;
		String hardware = android.os.Build.HARDWARE;
		String model = android.os.Build.MODEL;
		String product = android.os.Build.PRODUCT;
		if (brand.equalsIgnoreCase("generic")
				|| device.equalsIgnoreCase("generic")
				|| model.equalsIgnoreCase("sdk")
				|| product.equalsIgnoreCase("sdk")
				|| hardware.equalsIgnoreCase("goldfish")) {
			CLog.d(TAG, "Find Emulator by EmulatorBuild!");
			return true;
		}
		CLog.d(TAG, "Not Find Emulator by EmulatorBuild!");
		return false;
	}

	public static boolean checkOperatorNameAndroid(Context context) {
		String szOperatorName = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkOperatorName();

		if (szOperatorName.equalsIgnoreCase("android")) {
			CLog.d(TAG, "Find Emulator by OperatorName!");
			return true;
		}
		CLog.d(TAG, "Not Find Emulator by OperatorName!");
		return false;
	}
}
